package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrdersettingService;
import com.itheima.health.utils.POIUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @program: health_parent
 * @description: 订单controller
 * @author: HuChunping
 * @create: 2021-01-12 20:42
 **/
@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    private static final Logger log = LoggerFactory.getLogger(OrderSettingController.class);

    @Reference
    private OrdersettingService ordersettingService;

    /**
    * @Description: 批量导入预约Excel表格
    * @Param: [execelFile]
    * @return: com.itheima.health.entity.Result
    * @Author: HuChunping
    * @Date: 2021/1/13-17:00
    */
    @PostMapping("/upload")
    public Result upload(MultipartFile excelFile){
        try {
            // 读取excel文件，其中的String[]代表excel的每一行，List[0]代表第一行
            List<String[]> excelData = POIUtils.readExcel(excelFile);
            log.debug("导入预约设置读取到了{}行记录", excelData.size());
            final SimpleDateFormat sdf = new SimpleDateFormat(POIUtils.DATE_FORMAT);
            // 将List<String>转成 List<OrderSetting>
            // String[]长度为2，0:日期，1：数量
            List<OrderSetting> orderSettingList = excelData.stream().map(arr -> {
                OrderSetting orderSetting = new OrderSetting();
                // 返回orderSetting,这里arr是原集合的参数(String[])，map映射只有collect之后才生效
                try {
                    // 将数组String[]封装成orderSetting实体类
                    orderSetting.setOrderDate(sdf.parse(arr[0]));
                    orderSetting.setNumber(Integer.valueOf(arr[1]));
                } catch (ParseException e) {
                    log.debug("服务器内部错误");
                }
                return orderSetting;
            }).collect(Collectors.toList());
            // 批量导入
            ordersettingService.addbatch(orderSettingList);
            // 不报错，导入成功
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (IOException e) {
            log.error(MessageConstant.IMPORT_ORDERSETTING_FAIL);
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

    /**
    * @Description: 通过年月查询当月数据
    * @Param: [month]
    * @return: com.itheima.health.entity.Result
    * @Author: HuChunping
    * @Date: 2021/1/14-19:43
    */
    @GetMapping("/getOrderSettingByMonth")
    public Result getDataByMonth(String month){
        // 没有对应的实体类，将结果封装到map中，最大预约数：number；已预约人数：reservations
        List<Map<String,Integer>> data = ordersettingService.getOrderSettingByMonth(month);
        return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS, data);
    }

    /**
    * @Description: 通过日期更新最大预约数
    * @Param: [orderSetting]
    * @return: com.itheima.health.entity.Result
    * @Author: HuChunping
    * @Date: 2021/1/14-21:43
    */
    @PostMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        ordersettingService.editNumberByDate(orderSetting);
        return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
    }
}














